package br.com.lazyprogrammer.worldsoccermanager.fragments.create;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import br.com.lazyprogrammer.worldsoccermanager.R;
import br.com.lazyprogrammer.worldsoccermanager.fragments.interfaces.OnNextListener;
import br.com.lazyprogrammer.worldsoccermanager.localization.LocalizationDAO;
import br.com.lazyprogrammer.worldsoccermanager.localization.LocalizationFactoryDAO;
import br.com.lazyprogrammer.worldsoccermanager.localization.exception.GPSNotAvailableException;
import br.com.lazyprogrammer.worldsoccermanager.network.model.Stadium;
import br.com.lazyprogrammer.worldsoccermanager.system.generator.Generator;
import br.com.lazyprogrammer.worldsoccermanager.system.message.Message;
import br.com.lazyprogrammer.worldsoccermanager.util.PoolData;

/**
 * Created by Andre on 08/09/2014.
 */
public class SelectTeamStadiumFragment extends Fragment implements View.OnClickListener, LocalizationDAO.LocationChangedListener {

    private Button buttonNext, buttonSetStadiumLocation;
    private EditText inputStadiumName, inputStadiumTicket;
    private LinearLayout layoutFindLocation;

    private Location stadiumLocation;
    private LocalizationDAO localizationDAO;

    private OnNextListener listener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        listener = (OnNextListener) activity;
    }

    @Override
    public void onClick(View view) {
        if (view.equals(buttonNext))
            next();
        else if (view.equals(buttonSetStadiumLocation))
            getLocation();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_create_team_step01, container, false);
    }

    @Override
    public void onLocationChanged(Location location) {
        stadiumLocation = location;

    }

    @Override
    public void onResume() {
        super.onResume();

        buttonNext = (Button) getView().findViewById(R.id.button_next);
        buttonSetStadiumLocation = (Button) getView().findViewById(R.id.button_set_stadium_location);

        inputStadiumName = (EditText) getView().findViewById(R.id.input_stadium_name);
        inputStadiumTicket = (EditText) getView().findViewById(R.id.input_ticket);

        layoutFindLocation = (LinearLayout) getView().findViewById(R.id.layout_location);
    }

    @Override
    public void onTimeout() {

    }

    private void getLocation() {
        try {
            localizationDAO = LocalizationFactoryDAO.create(getActivity());
            localizationDAO.requestLocationOnce(getActivity(), this);
        } catch (GPSNotAvailableException e) {

        }
    }

    private String getStadiumNameInputText() {
        return inputStadiumName.getText().toString();
    }

    private String getStadiumTicketInputText() {
        return inputStadiumTicket.getText().toString();
    }

    private boolean isStadiumNameInputFilled() {
        return !getStadiumNameInputText().trim().equals("");
    }

    private boolean isStadiumTicketInputFilled() {
        return !getStadiumTicketInputText().trim().equals("");
    }

    private void next() {
        if (isStadiumNameInputFilled() && isStadiumTicketInputFilled()) {
            String stadiumId = Generator.encodeCustomId(getStadiumNameInputText(), System.currentTimeMillis(), getResources().getConfiguration().locale);
            Stadium stadium = new Stadium(stadiumId, getStadiumNameInputText());
            PoolData.team.setStadium(new Stadium());
        }
        else
            Message.send(getActivity(), R.string.exception_empty_field);
    }

}
